import { ResponseData, SshLinkInfoTree, SshSubMenu } from '@/entity/entitys';
import { invoke } from '@tauri-apps/api/core';
import { useTagsViewStore } from '@/store/modules/SshtagsView';
import { SendSystemNotification } from '@/notification/notification';
import { useI18n } from '@/hooks/web/useI18n'
const { t } = useI18n()
const tagsViewStore = useTagsViewStore();
export const getSshConnect = () => {
    invoke('connection_list').then((res): void => {
        let conn_map = new Map<string, SshLinkInfoTree>();
        let res_ = res as ResponseData<Array<SshSubMenu>>
        console.log("connection_list data: " + res_.data);
        console.log("connection_list code: " + res_.code);
        if (res_ == null) return;
        if (res_.code == 200 && res_.data != null) {
          let data = res_.data as Array<SshSubMenu>;
          for (let i = 0; i < data.length; i++) {
            let item = data[i];
            if (item.favorites_name != null && item.favorites_name != "" && conn_map.has(item.favorites_name)) {
              conn_map.get(item.favorites_name)?.subMenu.push(item);
            } else if (item.favorites_name != null && item.favorites_name != "" && item != null) {
              let sshLinkInfoTree = new SshLinkInfoTree();
              sshLinkInfoTree.favorites_name = item.favorites_name;
              sshLinkInfoTree.subMenu.push(item);
              conn_map.set(item.favorites_name, sshLinkInfoTree);
            }
          }
          if (tagsViewStore.getSshLinkInfoTree.length > 0 && conn_map.size > 0) {
            tagsViewStore.ClearSshLinkInfoTree();
          }
          conn_map.forEach((value, key) => {
            tagsViewStore.setSshLinkInfoTree(value);
          });
        } else {
          SendSystemNotification(t('appName') ? t('appName') : "notify", res_.msg)
        }
      })
      
}